package com.zegome.game.oanquan.gamelogic;

import com.zegome.game.oanquan.scene.play.BoardManager;
import com.zegome.game.oanquan.scene.play.PondData;

/**
 * (C) 2013 ZeroTeam
 * @author QuanLT
 */

public class Node {
	TempPond[] mTempPonds;
	
	Action mPreAction;
	
	int[] mScores;
	int mCurrentFlag;
	int mCurrentDepth;
	
	Node(final TempPond[] temp, final int flag, final int...scores) {
		mTempPonds = new TempPond[BoardManager.TOTAL_POND];
		initArray(temp, true);
		mScores = new int[2];
		setScore(scores);
		mCurrentDepth = 0;
		mPreAction = new Action();
		mCurrentFlag = flag;
	}
	
	TempPond[] createTempPonds() {
		final TempPond[] temp = new TempPond[BoardManager.TOTAL_POND];
		for (int i = 0; i < BoardManager.TOTAL_POND; i++) {
			temp[i] = new TempPond(i % 6 == 0);
			temp[i].deepCopy(mTempPonds[i]);
		}
		return temp;
	}
	
	void initArray(final TempPond[] ponds, boolean isContructor) {
		if (ponds == null && isContructor) {
			return;
		}
		for (int i = 0; i < BoardManager.TOTAL_POND; i++) {
			mTempPonds[i] = new TempPond(i % 6 == 0);
			mTempPonds[i].deepCopy(ponds[i]);
		}
	}
	
	void setScore(final int[] scores) {
		if (scores == null) return;

		for (int i = 0; i < 2; i++) {
			mScores[i] = scores[i];
		}
	}
	
	void setAction(final int index, final int direction) {
		mPreAction.mIndex = index;
		mPreAction.mDirection = direction;
	}
	
	void setScore(final int max, final int min) {
		mScores[0] = min;
		mScores[1] = max;
	}
	
	int getValue() {
		return mScores[1] - mScores[0];
	}
	
	void addScore(final int max, final int min) {
		mScores[0] += min;
		mScores[1] += max;
	}
			
	void copyValue(final Node temp) {
		for (int i = 0; i < BoardManager.TOTAL_POND; i++) {
			mTempPonds[i].deepCopy(temp.mTempPonds[i]);
		}
		setScore(temp.mScores[1], temp.mScores[0]);
		mCurrentDepth = temp.mCurrentDepth;
	}
	
	void copyValue(final PondData[] ponds) {
		for (int i = 0; i < BoardManager.TOTAL_POND; i++) {
			mTempPonds[i].deepCopy(ponds[i]);
		}
	}
}
